/**
 * Flii.as
 * 
 * Handles incoming date from the wiimote via a haXe/swHx backend
 **/

 
package com.freesome.flii
{
	
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher; 
	import com.freesome.flii.events.*;
	
	//import sockets
	import flash.net.Socket;
	
	import swhx.*;
	import flash.utils.*;


	
	public class Flii implements IEventDispatcher
	{
		
	    static private var instance:Flii;
	    static public var inSWHX:Boolean = false;
	    static public var currData:Number = 0;
    	private var dispatcher:EventDispatcher;
    	/** 
    	 * current PositionData
    	 **/
    	 static public var WiiRemoteACx:Number = 128;
    	 static public var WiiRemoteACy:Number = 128;
    	 static public var WiiRemoteACz:Number = 128;
      	 static public var NunchuckACx:Number = 128;
    	 static public var NunchuckACy:Number = 128;
    	 static public var NunchuckACz:Number = 128;
    	 
    	 static public var NunchuckJOYx:Number = 128;
    	 static public var NunchuckJOYy:Number = 128;
    	
    	  
    		
		/**Button constants
		 * 
		 **/
		public static const WiiRemoteAButton:Number = 0;
		public static const WiiRemoteBButton:Number = 1;
		public static const WiiRemoteOneButton:Number = 2;
		public static const WiiRemoteTwoButton:Number = 3;
		public static const WiiRemoteMinusButton:Number = 4;
		public static const WiiRemoteHomeButton:Number = 5;
		public static const WiiRemotePlusButton:Number = 6;
		public static const WiiRemoteUpButton:Number = 7;
		public static const WiiRemoteDownButton:Number = 8;
		public static const WiiRemoteLeftButton:Number = 9;
		public static const WiiRemoteRightButton:Number = 11;		
		public static const WiiNunchukZButton:Number = 12;
		public static const WiiNunchukCButton:Number = 13;
		
		/**
		 * Calibration data
		 * 
		 **/
		public static var WiiRemoteAccX_zero:Number = 127;
		public static var WiiRemoteAccY_zero:Number = 127;
		public static var WiiRemoteAccZ_zero:Number  = 127;
		public static var WiiRemoteAccX_1g:Number  = 127;
		public static var WiiRemoteAccY_1g:Number  = 127;
		public static var WiiRemoteAccZ_1g:Number  = 127;
		public static var WiiNunchukAccX_zero:Number  = 127;
		public static var WiiNunchukAccY_zero:Number  = 127;
		public static var WiiNunchukAccZ_zero:Number  = 127;
		public static var WiiNunchukAccX_1g:Number  = 127;
		public static var WiiNunchukAccY_1g:Number  = 127;
		public static var WiiNunchukAccZ_1g:Number  = 127;
		public static var WiiNunchukx_max:Number = 127;
		public static var WiiNunchuky_max:Number = 127;
		public static var WiiNunchukx_min:Number = 127;
		public static var WiiNunchuky_min:Number = 127;
		public static var WiiNunchukx_center:Number = 127;
		public static var WiiNunchuky_center:Number = 127;
		

		

		/**
		 * Returns a reference to the class.  Use for accessing addEventListener
		 **/
		 
		 
		static public function getInstance():Flii {
	      if(instance == null) {
	      	
	        instance = new Flii();
	        instance.dispatcher = new EventDispatcher();
	      }
	      
	      	return instance;
	    }
		static public function Log(str:String):void {
			instance.fliiData(str);
		}
		
		/**
		 * Receive raw socket data command
		 **/		 
		static public function rcSocket(data:String):void {
			var a:Array = data.split(":");
			switch(a[0]) {
				case "ac":
					instance.fliiAC(a[1],a[2],a[3],a[4]);
					break;
				case "ir":
					instance.fliiIR(a[1],a[2]);
					break;
				case "js":
					instance.fliiJS(a[1],a[2],a[3]);
					break
				default:
					Log(data);
			}
			
		}
	    
	    /**
	    * Implement IEventDispatcher
	    * 
	    **/
	    
	    public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
	        dispatcher.addEventListener(type, listener, useCapture, priority);
	    }	
	    public  function dispatchEvent(evt:Event):Boolean{
	        return dispatcher.dispatchEvent(evt);
	    }
	    
	    public function hasEventListener(type:String):Boolean{
	        return dispatcher.hasEventListener(type);
	    }
	    
	    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
	        dispatcher.removeEventListener(type, listener, useCapture);
	    }
	                   
	    public function willTrigger(type:String):Boolean {
	        return dispatcher.willTrigger(type);
	    }

	
		public   function fliiBUT(type:int, pressed:Boolean):void {
			instance.dispatchEvent( new FliiEvent("onBUT",true,true,{type:type, pressed:pressed}));
		}
		public  function fliiData(s:String):void{
			instance.dispatchEvent( new FliiEvent("onDATA",true,true,{msg:s}));
			
		}

		public  function fliiIR(x:Number,y:Number):void {
			 instance.dispatchEvent( new FliiEvent("onIR",true,true,{x:x,y:y}));
			 
			
		}
		public function rcInts(a:Array) {
			fliiData(a[0].toString());
		}
		public  function fliiAC(type:int,x:int,y:int,z:int):void {
				if (type==0) {
					 WiiRemoteACx=x;
					 WiiRemoteACy=y;    			
    				 WiiRemoteACx=z;	
    			} else {
    				NunchuckACx=x;
					NunchuckACy=y;    			
    				NunchuckACx=z;		
    			}
   				
			 instance.dispatchEvent( new FliiEvent("onAC",true,true,{type:type,x:x,y:y,z:z}));
			
		}		

		public  function fliiJS(type:int,x:int,y:int):void {
			if (type==0) {
				NunchuckJOYy=y;
    			NunchuckJOYx=x;
    			dispatchEvent( new FliiEvent("onJOY",true,true,{type:type,x:x,y:y})); 
   			}
			
			
		}
		public  function fliiACCAB( type: int, accX_zero: int,  accY_zero: int, accZ_zero : int, accX_1g : int,accY_1g : int, accZ_1g : int):void {
			switch(type) {

				case 0:
					WiiRemoteAccX_zero=accX_zero;
					WiiRemoteAccY_zero=accY_zero;
					WiiRemoteAccZ_zero=accZ_zero;
					WiiRemoteAccX_1g=accX_1g;					
					WiiRemoteAccY_1g=accY_1g;
					WiiRemoteAccZ_1g=accZ_1g;	
					instance.dispatchEvent( new FliiEvent("onWiiCalibrate",true,true,{}));
					break;			
				case 1:
					WiiNunchukAccX_zero=accX_zero;
					WiiNunchukAccY_zero=accY_zero;
					WiiNunchukAccZ_zero=accZ_zero;
					WiiNunchukAccX_1g=accX_1g;					
					WiiNunchukAccY_1g=accY_1g;
					WiiNunchukAccZ_1g=accZ_1g;	
					instance.dispatchEvent( new FliiEvent("onNunchuckCalibrate",true,true,{}));
					break;				
					
			}
		
		}
		public  function fliiJOYCAB( type:int,x_max:int, x_min:int, x_center:int, y_max:int, y_min:int, y_center:int):void {
			WiiNunchukx_max = x_max;
			WiiNunchuky_max = y_max;
			WiiNunchukx_min = x_min;
			WiiNunchuky_max = y_min
			WiiNunchukx_center = x_center;
			WiiNunchuky_center = y_center;		
			instance.dispatchEvent( new FliiEvent("onNunchuckCalibrate",true,true,{}));
		}
		
	}
}